<?php


namespace app\manage\controller;

use app\manage\model\Token;
use think\Db;
use think\Validate;
class Rbac
{
    public function __construct()
    {
        $this->token=new Token();
        $this->rbac=new \app\manage\model\Rbac();
    }

    /**
     * 角色列表
     */
    public function index(){
        $data=$this->rbac->index();

        returnJson(200,'success',$data);
    }

    //添加角色
    public function roleAdd(){
        $param=input();
        $validate = new Validate([
            ['name', 'require|max:15', '角色名称不能为空|角色名称长度不能超过15'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }

        $this->rbac->Add($param);

        returnJson(200,'操作成功');
    }

    //删除角色
    public function roleDel(){
        $id=input('r_id');

        $this->rbac->del($id);

        returnJson(200,'操作成功');
    }

    //编辑角色页面
    public function roleEdit(){

        $id=input('r_id');

        $data=$this->rbac->detail($id);

        returnJson(200,'success',$data);
    }

    //编辑提交
    public function roleSubmit(){
        $param=input();
        $validate = new Validate([
            ['name', 'require|max:15', '角色名称不能为空|角色名称长度不能超过15'],
            ['r_id', 'require', '角色id不能为空'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }

        $this->rbac->Add($param);

        returnJson(200,'操作成功');
    }

    /**
     * 角色分配权限
     */
    public function authorize(){
        $roleId=input('r_id');
        if (empty($roleId)) {
            returnJson(-2004,'参数错误');
        }

        $adminMenu=Db::name('admin_menu')
            ->order("list_order", "ASC")
            ->column('*');

        $privilegeData = Db::name("AuthAccess")
            ->where("role_id", $roleId)
            ->column("rule_name");//获取权限表数据

        foreach ($adminMenu as $m) {
            $newMenus[$m['am_id']] = $m;
        }

        foreach ($adminMenu as $n => $t) {
            //checked  1=选中  2=不选中
            $adminMenu[$n]['checked'] = ($this->_isChecked($t, $privilegeData)) ? 1 : 2;
            $adminMenu[$n]['level'] = $this->_getLevel($t['am_id'], $newMenus);
        }

        returnJson(200,'success',$adminMenu);
    }

    /**
     * 检查指定菜单是否有权限
     * @param array $menu menu表中数组
     * @param       $privData
     * @return bool
     */
    private function _isChecked($menu, $privData)
    {
        $app    = $menu['app'];
        $model  = $menu['controller'];
        $action = $menu['action'];
        $name   = strtolower("$app/$model/$action");
        if ($privData) {
            if (in_array($name, $privData)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }

    }


    /**
     * 获取菜单深度
     * @param       $id
     * @param array $array
     * @param int   $i
     * @return int
     */
    protected function _getLevel($id, $array = [], $i = 0)
    {
        if ($array[$id]['parent_id'] == 0 || empty($array[$array[$id]['parent_id']]) || $array[$id]['parent_id'] == $id) {
            return $i;
        } else {
            $i++;
            return $this->_getLevel($array[$id]['parent_id'], $array, $i);
        }
    }

}